package com.qinge.blog.service.impl;

import com.qinge.blog.dao.BlogCategoryDao;
import com.qinge.blog.dao.BlogContentDao;
import com.qinge.blog.entity.*;
import com.qinge.blog.service.BlogCategoryService;
import com.qinge.blog.service.BlogContentService;
import com.qinge.blog.vo.BlogContentVO;
import org.springframework.stereotype.Service;
import com.qinge.blog.dao.BlogContentCategoryDao;
import com.qinge.blog.service.BlogContentCategoryService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author QDW
 * @date 2022-05-02 23:38:51
 * @description BlogContentCategory service层实现类
 */
@Service
public class BlogContentCategoryServiceImpl  implements BlogContentCategoryService {

    @Resource
    BlogContentCategoryDao dao;

    @Resource
    BlogCategoryDao categoryDao;

    @Resource
    BlogContentDao contentDao;

    @Override
    public void add(BlogContentCategory... contentCategories) {
        dao.batchInsert(Arrays.asList(contentCategories));
    }

    @Override
    public void addByList(List<BlogContentCategory> contentCategories) {
        dao.batchInsert(contentCategories);
    }

    @Override
    public void removeByConIds(String ... conIds) {
        removeByConIdList(Arrays.asList(conIds));
    }

    @Override
    public void removeByCatIds(String ... catIds) {
        removeByCatIdList(Arrays.asList(catIds));
    }

    @Override
    public void removeByConIdList(List<String> conIds) {
        BlogContentCategoryExample example=new BlogContentCategoryExample();
        BlogContentCategoryExample.Criteria criteria=example.createCriteria();

        criteria.andContentIdIn(conIds);

        dao.deleteByExample(example);
    }

    @Override
    public void removeByCatIdList(List<String> catIds) {
        BlogContentCategoryExample example=new BlogContentCategoryExample();
        BlogContentCategoryExample.Criteria criteria=example.createCriteria();

        criteria.andContentIdIn(catIds);

        dao.deleteByExample(example);
    }

    @Override
    public String findCatIdByConId(String conId) {
        BlogContentCategoryExample example=new BlogContentCategoryExample();
        BlogContentCategoryExample.Criteria criteria=example.createCriteria();

        criteria.andContentIdEqualTo(conId);

        List<BlogContentCategory> blogContentCategories = dao.selectByExample(example);

        if(blogContentCategories.size()==0) {
            return null;
        }

        return blogContentCategories.get(0).getCategoryId();
    }

    @Override
    public List<String> findByCatId(String catId) {
        BlogContentCategoryExample example=new BlogContentCategoryExample();
        BlogContentCategoryExample.Criteria criteria=example.createCriteria();

        criteria.andContentIdEqualTo(catId);

        List<BlogContentCategory> blogContentCategories = dao.selectByExample(example);

        List<String> ids=blogContentCategories.stream().map(BlogContentCategory::getCategoryId).collect(Collectors.toList());

        return ids;

    }
}

